package com.ai.academiainteligente;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ai.WS.AIWS;
import com.ai.WS.AIWSTask;
import com.ai.util.AIActivity;
import com.ai.util.Academia;
import com.ai.util.AcademiaAdapter;
import com.ai.util.Modalidade;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;

public class ListAcademiaActivity extends Activity implements
		OnItemClickListener, AIActivity {
	ArrayList<Academia> academias;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_academia);
		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		AIWSTask conserveTask = new AIWSTask(this, AIWS.metodos.GetAcademias,
				progressBar);
		try {
			conserveTask.execute();
		} catch (Exception e) {
			conserveTask.cancel(true);
		}
		ListView grid = (ListView) findViewById(R.id.listView1);
		grid.setOnItemClickListener(this);
	}

	@Override
	public void retornoWS(JSONObject json) {
		try {
			academias = new ArrayList<Academia>();
			JSONArray array = json.getJSONArray("academias");
			for (int i = 0; i < array.length(); i++) {
				JSONObject objeto = array.getJSONObject(i);
				Academia academia = new Academia();
				academia.setCidade(objeto.getString("cidade"));
				academia.setEstado(objeto.getString("estado"));
				academia.setCnpj(objeto.getString("cnpj"));
				academia.setCep(objeto.getString("cep"));
				academia.setBairro(objeto.getString("bairro"));
				academia.setNumero(objeto.getString("numero"));
				academia.setEndereco(objeto.getString("endereco"));
				academia.setTelefone(objeto.getString("telefone"));
				academia.setFantasia(objeto.getString("fantasia"));
				academia.setId(objeto.getInt("id"));
				JSONArray modalidadesJSON = objeto.getJSONArray("modalidades");
				for (int j = 0; j < modalidadesJSON.length(); j++) {
					JSONObject modalidadeJson = modalidadesJSON
							.getJSONObject(j);
					Modalidade modalidade = new Modalidade();
					modalidade.setDescricao(modalidadeJson
							.getString("descricao"));
					academia.getModalidades().add(modalidade);

				}
				academias.add(academia);

			}
			ListView grid = (ListView) findViewById(R.id.listView1);
			grid.setAdapter(new AcademiaAdapter(academias));
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_academia, menu);
		return true;
	}

	@Override
	public Activity getActivity() {

		return this;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(this, AcademiaActivity.class);
		intent.putExtra("academia", academias.get(position));
		startActivity(intent);
	}

}
